package com.ks.app.admin.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.ks.app.admin.hibernate.KsVersion;
import com.ks.app.admin.service.*;
import com.ks.app.admin.util.Pager;
import com.ks.app.search.util.Constants;
import java.io.File;    
import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;


@ParentPackage("struts-default")
@Namespace("")
@InterceptorRefs( {
	@InterceptorRef("defaultStack"),
	@InterceptorRef(value = "fileUpload", params = {"allowedTypes","image/jpeg,image/gif,image/png","maximumSize", "97867700" })
})  
@Results( {
		@Result(name = "success", location = "/WEB-INF/admin/main.html"),
		@Result(name = "quit", location = "/WEB-INF/admin/send.jsp"),
		@Result(name = "error", location = "/WEB-INF/admin/error.jsp"),
		@Result(name = "listver", location = "/WEB-INF/admin/Version/listver.jsp"),
		@Result(name = "addApk", location = "/WEB-INF/admin/Version/addApk.jsp")
})
public class KsVersionAction implements ServletRequestAware{
	private HttpServletRequest request;
	private String username;	
	private File reso;	
	private String resoFileName;	
	private KsVersion apkinfo;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Resource
	private KsVersionService ksVersionService;
	
	
	public String getResoFileName() {
		return resoFileName;
	}
	public void setResoFileName(String resoFileName) {
		this.resoFileName = resoFileName;
	}
	public File getReso() {
		return reso;
	}
	public void setReso(File reso) {
		this.reso = reso;
	}
	public KsVersion getApkinfo() {
		return apkinfo;
	}
	public void setApkinfo(KsVersion apkinfo) {
		this.apkinfo = apkinfo;
	}	
		
	private HttpSession session;
	public HttpSession getSession() {
		return session;

	}
	public void setSession(HttpSession session) {
		this.session = session;
	}	
	
	public void setServletRequest(HttpServletRequest arg0) {
		request=arg0;
	}

	@Action("ks_version_grid")
	public String findVersionList()//根据条件获得版本内容 分页展示
	{		
		 try {
			 
			int  pno         = request.getParameter("pno")   == null?1:Integer.parseInt(request.getParameter("pno"));
			int  vtype       = request.getParameter("vtype") == null?0:Integer.parseInt(request.getParameter("vtype"));
			int  status      = request.getParameter("status")  == null?0:Integer.parseInt(request.getParameter("status"));
			
			KsVersion kv = new KsVersion();
			kv.setVtype(vtype);
			kv.setStatus(status);
		
			Pager pager = new Pager();
			pager.setCntOfPage(20);
			pager.setCurPage(pno);
			
			List<KsVersion> querylist = ksVersionService.findKsVersionList(vtype, status, pager);
			
			if(pno>1 && (querylist==null || querylist.size()==0)){
				
				pager.setCurPage(pno-1);
				querylist = ksVersionService.findKsVersionList(vtype, status, pager);
				
			}else if(pno==1 && (querylist==null || querylist.size()==0)){
				pager.setCurPage(0);
			}
			
			request.setAttribute("list", querylist);
			request.setAttribute("kv", kv);
			request.setAttribute("PAGER", pager);
			
			return "listver";
			
		}
		 catch (Exception e) 
		{
			 e.printStackTrace();
			return Error();
		}
	}
	
	@Action(value="ks_del_version")
	public String delVersion(){
		
		username = String.valueOf(request.getSession().getAttribute("userinfo"));
		String ids = request.getParameter("id") == null?"":request.getParameter("id");
		ksVersionService.deleteKsVersion(ids, username);
		return findVersionList();
		
	}
	
	@Action("addApk")
	public String addApk()//进入添加版本页面
    {  		
		return "addApk";    	
    }	
	
	@Action("/admin/Version/Error")
	public String Error()//抛错跳转页面
	{			
		return "error";
	}	

	
	@Action("saveapk")
	public String SaveApk()//保存版本
    {   		
		try {			
			if (reso == null)
			{	
				return Error();
			}
			String path = Constants.CONS_PROPERTIES.getValue("filedir");
			File targeta = new File(path+"/Apk/"+apkinfo.getVersion()+"/",resoFileName);				
			FileUtils.copyFile(reso, targeta);					
			apkinfo.setFilepath(path+"/Apk/"+apkinfo.getVersion()+"/"+resoFileName);		
			apkinfo.setVtype(1);
			apkinfo.setCreatetime(sdf.format(new Date()));
			apkinfo.setStatus(2);
			username=request.getSession().getAttribute("userinfo").toString().trim();
			long apkid=ksVersionService.addKsVersion(apkinfo, username);
			if(apkid<=0)
			{
				return Error();
			}
			return  findVersionList();
		}
		
		catch (Exception e) 
		{
			return Error();
		}   	
    }

}